﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Content.cs" company="Anton Frattaroli">
//   Copyright (c) Anton Frattaroli. All rights reserved.
// </copyright>
// <summary>
//   Defines the Content class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Lidocaine.Data
{
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Globalization;
    using Lidocaine.BusinessObjects;

    /// <summary>
    /// Defines the Content class.
    /// </summary>
    public static class Content
    {
        /// <summary>
        /// Selects a content based on the identifier.
        /// </summary>
        /// <param name="id">The content identifier.</param>
        /// <returns>An instantiated content.</returns>
        public static BusinessObjects.Content Select(int id)
        {
            LazyContent content = null;
            using (SqlConnection conn = new SqlConnection(Environment.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.GetContentById", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@ContentID", id);
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        if (rdr.HasRows)
                        {
                            rdr.Read();
                            content = new LazyContent();
                            content.Id = id;
                            content.PageId = Convert.ToInt32(rdr["PageID"], CultureInfo.InvariantCulture);
                            content.AuthorId = Convert.ToInt32(rdr["UserID"], CultureInfo.InvariantCulture);
                            content.Created = Convert.ToDateTime(rdr["Created"], CultureInfo.InvariantCulture);
                            content.Html = rdr["Content"].ToString();
                        }
                    }
                }
            }

            return content;
        }

        /// <summary>
        /// Inserts a content object.
        /// </summary>
        /// <param name="content">The content to insert.</param>
        /// <returns>The inserted Content with a newly populated Id field.</returns>
        public static BusinessObjects.Content Insert(BusinessObjects.Content content)
        {
            using (SqlConnection conn = new SqlConnection(Environment.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.InsertPageContent", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@PageID", content.PageId);
                    cmd.Parameters.AddWithValue("@PageContent", content.Html);
                    cmd.Parameters.AddWithValue("@UserID", content.AuthorId);
                    content.Id = Convert.ToInt32(cmd.ExecuteScalar(), CultureInfo.InvariantCulture);
                }
            }

            return content;
        }
    }
}
